package com.example.testdemo.manager;

import org.andengine.entity.scene.Scene;

import android.util.Log;
import android.util.SparseArray;

import com.example.testdemo.TankGameActivity;
import com.example.testdemo.scenes.BaseScene;
import com.example.testdemo.scenes.SplashScene;
import com.example.testdemo.scenes.menu.BaseMenu;
import com.example.testdemo.scenes.menu.MenuInGameScene;
import com.example.testdemo.scenes.stage.Stage1;

public class ScenesManager {
	private static final String TAG = "SceneManager";

	public static final int SCENE_SPLASH = 1;
	public static final int SCENE_LOADING = 2;
	
	public static final int SCENE_STAGE_1 = 10;
	public static final int SCENE_STAGE_2 = 11;

	public static final int MENU_IN_GAME = 100;

	private static ScenesManager INSTANCE = null;

	private int mCurrentScene = SCENE_SPLASH;
	private SparseArray<BaseScene> mScenesMap = new SparseArray<BaseScene>();
	private SparseArray<BaseMenu> mMenusMap = new SparseArray<BaseMenu>();

	public static ScenesManager getInstance() {
		if (INSTANCE == null) {
			INSTANCE = new ScenesManager();
		}
		return INSTANCE;
	}

	public void setCurrentScene(Scene scene) {
		if (scene.equals(mScenesMap.get(SCENE_STAGE_1))) {
			mCurrentScene = SCENE_STAGE_1;
		} else if (scene.equals(mScenesMap.get(SCENE_SPLASH))) {
			mCurrentScene = SCENE_SPLASH;
		} else if (scene.equals(mScenesMap.get(SCENE_LOADING))) {
			mCurrentScene = SCENE_LOADING;
		}
	}

	public int getCurrentSceneIndex() {
		return mCurrentScene;
	}

	public BaseScene getScene(int whichScene) {
		return mScenesMap.get(whichScene);
	}

	public BaseMenu getMenu(int which) {
		return mMenusMap.get(which);
	}

	public void loadMenu(TankGameActivity activity, int which, int fatherScene) {
		switch (which) {
		case MENU_IN_GAME:
			Log.d(TAG, "load menu in game ...");
			BaseMenu menu = new MenuInGameScene(CamerasManager.getInstance()
			        .getCamera(CamerasManager.CAMERA_ZOOM), fatherScene);
			menu.createScene(activity);
			mMenusMap.put(which, menu);
			Log.d(TAG, "load menu in game done");
			break;
		}
	}

	public void loadScene(TankGameActivity activity, int whichScene) {
		switch (whichScene) {
		case SCENE_SPLASH:
			Log.d(TAG, "load splash scene ...");
			BaseScene splash = new SplashScene();
			splash.createScene(activity);
			mScenesMap.put(whichScene, splash);
			Log.d(TAG, "load splash scene done");
			break;
		case SCENE_STAGE_1:
			Log.d(TAG, "load stage 1 Scene ...");
			BaseScene demo = new Stage1();
			demo.createScene(activity);
			mScenesMap.put(whichScene, demo);
			Log.d(TAG, "load demo scene done");
			break;
		}
	}

	public void unloadScene(int whichScene) {
		switch (whichScene) {
		case SCENE_SPLASH:
			Log.d(TAG, "unload splash scene ...");
			break;
		case SCENE_STAGE_1:
			Log.d(TAG, "unload stage 1 scene ...");
			break;
		default:
			return;
		}

		BaseScene scene = mScenesMap.get(whichScene);
		if (scene != null) {
			scene.disposeScene();
			mScenesMap.remove(whichScene);
		}
	}

	public void unloadMenu(int which) {
		switch (which) {
		case MENU_IN_GAME:
			Log.d(TAG, "unload menu in game ...");
			break;
		default:
			return;
		}

		BaseMenu menu = mMenusMap.get(which);
		if (menu != null) {
			menu.disposeScene();
			mMenusMap.remove(which);
		}
	}

	public void free() {
		unloadMenu(MENU_IN_GAME);
		unloadScene(SCENE_SPLASH);
		unloadScene(SCENE_STAGE_1);
		unloadScene(SCENE_LOADING);
		mMenusMap.clear();
		mScenesMap.clear();
		INSTANCE = null;
	}
}
